<?php
class CodeSpace_Oauth_Server_Request_Token_Access extends CodeSpace_Oauth_Server_Request_Abstract
{

    public function __construct ($options = null)
    {
        if (! is_null($options))
        {
            if (array_key_exists('oauth_token', $options))
            {
                $this->_token = new Zend_Oauth_Token_Access();
                $this->_token->setToken($options['oauth_token']);
                
                $tokenTable = new Model_DbTable_Tokens();
                $resultSet = $tokenTable->find($this->_token->getToken());
                if($resultSet->count() != 1)
                {
                    throw new Exception('Invalid Request Token');
                }
                $requestToken = $resultSet->current();
                $this->_token->setTokenSecret($requestToken->tkn_token_secret);
                unset($options['oauth_token']);
            }
        }
        parent::__construct($options);
    }

    public function createAccessToken ()
    {
        $accessToken = sha1(uniqid(rand(), true));
        $accessTokenSecret = sha1(uniqid(rand(), true));
        $this->_token = new Zend_Oauth_Token_Request();
        $this->_token->setToken($accessToken);
        $this->_token->setTokenSecret($accessTokenSecret);
    }
    
    public function buildControlSignature(CodeSpace_Oauth_Server_Config $config)
    {
        /*
         * Order for access token
         * 
         * oauth_consumer_key
         * oauth_nonce
         * oauth_signature_method
         * oauth_timestamp
         * oauth_token
         * oauth_version
         * oauth_signature
         */
        $params = array ();
        $params['oauth_consumer_key'] = $config->getConsumer()->getKey();
        $params['oauth_nonce'] = $config->getNonce();
        $params['oauth_signature_method'] = $config->getSignatureMethod();
        $params['oauth_timestamp'] = $config->getTimestamp();
        $params['oauth_token'] = $config->getToken()->getToken();
        $params['oauth_version'] = $config->getVersion();
        
        foreach ($config->getUserParams() as $key => $value)
        {
            $params[$key] = $value;
        }
                
        $utility = new Zend_Oauth_Http_Utility;
        $sign = $utility->sign(
            $params,
            $config->getSignatureMethod(),
            $config->getConsumer()->getSecret(),
            $config->getToken()->getTokenSecret(),
            'POST',
            'http://morpheus.gateway.mkb.com:41000/oauth/accesstoken'
        );
        
        return $sign;
    }
    
    public function process(CodeSpace_Oauth_Server $server)
    {
        $requestToken = $server->getToken();
        
        $accessToken = sha1(uniqid(rand(), true));
        $accessTokenSecret = sha1(uniqid(rand(), true));
        
        $tokenTable = new Model_DbTable_Tokens();
        $row = $tokenTable->find($requestToken->getToken())->current();
        $row->tkn_token = $accessToken;
        $row->tkn_token_secret = $accessTokenSecret;
        $row->tkn_state = 'ACCESS';
        $row->save();
        
        $owners = new Model_DbTable_ResourceOwners();
        $rowSet = $owners->find($requestToken->getToken());
        
        foreach($rowSet as $row)
        {
            $row->rown_token = $accessToken;
            $row->save();
        }
        
        $front = Zend_Controller_Front::getInstance();
        $front  ->getResponse()
                ->setHttpResponseCode(200)
                ->appendBody('oauth_token='.$accessToken.'&oauth_token_secret=' . $accessTokenSecret)
                ->sendResponse();
        exit;
    }
}
?>